﻿using GrapeCity.Forguncy.CellTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vant.WpfControls.DrawingObject
{
    /// <summary>
    /// StepperDrawingObject.xaml 的交互逻辑
    /// </summary>
    public partial class StepperDrawingObject : UserControl
    {
        public StepperDrawingObject(Vant.CellTypes.Stepper stepper, IDrawingHelper drawingHelper, ICellInfo cellInfo)
        {
            var viewModel = new StepperDrawingObjectViewModel();
            viewModel.CellStyle = StyleHelper.CreateStyleViewModel(cellInfo, drawingHelper);
            viewModel.CellStyle.Foreground = viewModel.CellStyle.Foreground ?? drawingHelper.GetBrush("#323233");
            viewModel.Text = cellInfo.Value?.ToString() ?? stepper.placeholder;
            viewModel.CellStyle.Foreground = string.IsNullOrEmpty(cellInfo.Value?.ToString()) ? Brushes.DarkGray : viewModel.CellStyle.Foreground;
            viewModel.ButtonSize = stepper.buttonSize;
            var themeColor = drawingHelper.GetBrush("Accent 1");
            viewModel.LeftButton.ButtonSize = stepper.buttonSize;
            viewModel.RightButton.ButtonSize = stepper.buttonSize;
            viewModel.LeftButton.Visibility = stepper.showMinus ? Visibility.Visible : Visibility.Collapsed;
            viewModel.RightButton.Visibility = stepper.showPlus ? Visibility.Visible : Visibility.Collapsed;
            if (stepper.theme == "rec")
            {
                viewModel.Background = drawingHelper.GetBrush("#f2f3f5");
                viewModel.LeftButton.Background = viewModel.Background;
                viewModel.RightButton.Background = viewModel.Background;
                viewModel.LeftButton.BorderBrush = viewModel.Background;
                viewModel.RightButton.BorderBrush = viewModel.Background;
                viewModel.LeftButton.SymbolColor = drawingHelper.GetBrush("#323233");
                viewModel.RightButton.SymbolColor = viewModel.LeftButton.SymbolColor;
                viewModel.LeftButton.CornerRadius = new CornerRadius(4, 0, 0, 4);
                viewModel.RightButton.CornerRadius = new CornerRadius(0, 4, 4, 0);
            }
            else
            {
                viewModel.LeftButton.Background = Brushes.Transparent;
                viewModel.RightButton.Background = themeColor;
                viewModel.LeftButton.BorderBrush = themeColor;
                viewModel.RightButton.BorderBrush = themeColor;
                viewModel.LeftButton.SymbolColor = themeColor;
                viewModel.RightButton.SymbolColor = Brushes.White;
                viewModel.LeftButton.CornerRadius = new CornerRadius(stepper.buttonSize / 2);
                viewModel.RightButton.CornerRadius = new CornerRadius(stepper.buttonSize / 2);
            }
            if (stepper.showInput)
            {
                viewModel.TextGridWidth = new GridLength(1, GridUnitType.Star);
                viewModel.TextVisiblity = Visibility.Visible;
            }
            else
            {
                viewModel.TextGridWidth = new GridLength(0);
                viewModel.TextVisiblity = Visibility.Collapsed;
            }

            this.DataContext = viewModel;
            if (stepper.IsDisabled)
            {
                Opacity = 0.5;
            }
            InitializeComponent();
        }
    }

    public class StepperDrawingObjectViewModel
    {
        public double Opacity { get; set; } = 1;
        public double ButtonSize { get; set; }
        public GridLength TextGridWidth { get; set; }
        public Brush Background { get; set; }
        public StepperButtonViewModel LeftButton { get; set; } = new StepperButtonViewModel();
        public StepperButtonViewModel RightButton { get; set; } = new StepperButtonViewModel();
        public Visibility TextVisiblity { get; set; }
        public string Text { get; set; }
        public CellTypeViewModelStyle CellStyle { get; set; }
    }

    public class StepperButtonViewModel
    {
        public CornerRadius CornerRadius { get; set; }
        public double ButtonSize { get; set; }
        public Visibility Visibility { get; set; }
        public Brush Background { get; set; }
        public Brush BorderBrush { get; set; }
        public Brush SymbolColor { get; set; }
        public double SymbolSize
        {
            get
            {
                return ButtonSize / 2;
            }
        }
    }
}
